package com.toman.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.View;


import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.IntentUtils;
import com.toman.Util.SystemUtils;
import com.toman.Util.ToastUtil;
import com.toman.ui.data.DataFragment;
import com.toman.ui.msg.MsgFragment;
import com.toman.ui.report.ReportFragment;
import com.toman.ui.serve.ServeFragment;
import com.toman.ui.work.WorkFragment;
import com.toman.ui.work.base.BeltlineInfActivity;
import com.toman.ui.work.base.TerminalInfActivity;
import com.toman.ui.work.base.UserInfActivity;
import com.toman.ui.work.scan.ScanDeviceEmptyActivity;
import com.toman.widget.BadgeUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * @Description:
 * @Author: 邹苏启
 * @Since: 2016/11/28 下午10:38
 */

public class HomeActivity extends TabHostActivity  {

    protected Class<?>[] tabFragments = {WorkFragment.class, DataFragment.class, MsgFragment.class, ReportFragment.class, com.toman.ui.my.MyFragment.class};
    // Tab选项卡的文字
    private int[] tabwidgetTags = {R.string.tab_work, R.string.tab_data, R.string.tab_msg, R.string.tab_report,R.string.tab_my};
    // Tab选项卡的按钮图片
    private int[] tabItemViews = {R.drawable.selector_home_tab_work, R.drawable.selector_home_tab_data, R.drawable.selector_home_tab_msg, R.drawable.selector_home_tab_serve, R.drawable.selector_home_tab_my};
    public Handler mHandler;
    MyApplication myApplication;

    public static final int REQUEST_CODE_PERMISSION = 100;
    public static final int REQUEST_CODE_CAMERA = 101;
    public static final int REQUEST_CODE_PHONE = 102;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
//SystemUtils.fullScreen(this);
        PushAgent.getInstance(this).onAppStart();
initPermmison();
        initHandler();
//        MobclickAgent.openActivityDurationTrack(false);
    }

    private void initPermmison() {

        try {
            AndPermission.with(getApplicationContext())
                    .requestCode(REQUEST_CODE_PERMISSION)
                    .permission(Permission.STORAGE,Permission.CAMERA)
                    .callback(permissionListener)
                    // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框；
                    // 这样避免用户勾选不再提示，导致以后无法申请权限。
                    // 你也可以不设置。
                    .rationale(null)
                    .start();
        } catch (Exception e) {
            e.printStackTrace();
        }

//        AndPermission.with(getApplicationContext())
//                .requestCode(REQUEST_CODE_CAMERA)
//                .permission(Permission.CAMERA)
//                .callback(permissionListener)
//                // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框；
//                // 这样避免用户勾选不再提示，导致以后无法申请权限。
//                // 你也可以不设置。
//                .rationale(null)
//                .start();
//        AndPermission.with(getApplicationContext())
//                .requestCode(REQUEST_CODE_PHONE)
//                .permission(Permission.PHONE)
//                .callback(permissionListener)
//                // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框；
//                // 这样避免用户勾选不再提示，导致以后无法申请权限。
//                // 你也可以不设置。
//                .rationale(null)
//                .start();
    }

    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION: {
                    break;
                }
            }
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION: {
                    ToastUtil.makeText(getApplicationContext(), "功能未授权，部分功能受限！");

                    break;

                }
                case REQUEST_CODE_CAMERA:
                    ToastUtil.makeText(getApplicationContext(), "相机未授权，部分功能受限！");

                    break;

//                case REQUEST_CODE_PHONE:
//                    ToastUtil.makeText(getApplicationContext(), "手机状态未授权，通知消息可能受限！");
//
//                    break;

            }
        }
    };





    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        BadgeUtil.resetBadgeCount(this,R.mipmap.baselogo);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);

    }


    //自定义toolbar 可以设置图标和功能


    /**
     * 设置返回键不关闭应用,回到桌面
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //启动一个意图,回到桌面
            Intent backHome = new Intent(Intent.ACTION_MAIN);
            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backHome.addCategory(Intent.CATEGORY_HOME);
            startActivity(backHome);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected Class<?>[] getTabFragment() {
        return tabFragments;
    }

    @Override
    protected int[] getTabwidgetDrawable() {
        return tabItemViews;
    }

    @Override
    protected int[] getTabwidgetTag() {
        return tabwidgetTags;
    }

    private void initHandler() {
        myApplication = (MyApplication) getApplication();

        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {

                    case 1:
                        try {
                            mTabHost.getTabWidget().getChildTabViewAt(2).findViewById(R.id.iv_red).setVisibility(View.VISIBLE);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break;

                    default:
                        break;
                }
            }
        };
        myApplication.setMsgHandler(mHandler);
    }
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        // 扫描二维码/条码回传
//        if (requestCode == SCANNING_CODE && resultCode == RESULT_OK) {
//            if (data != null) {
//                String content = data.getStringExtra(DECODED_CONTENT_KEY);
//                //Toast.makeText(getContext(), "扫描结果： " + content, Toast.LENGTH_SHORT).show();
//                Pattern pattern = Pattern.compile("\\s*/(device|user|terminal|beltline)/(\\d+)");
//                Matcher matcher = pattern.matcher(content);
//                if (matcher.find()) {
//                    switch (matcher.group(1)) {
//                        case "device":
//                            Bundle bundle = new Bundle();
//                            bundle.putString("id", matcher.group(2));
//                            IntentUtils.startActivity(this, ScanDeviceEmptyActivity.class, bundle);
//
//                            break;
//                        case "user":
//                            bundle = new Bundle();
//                            bundle.putString("id", matcher.group(2));
//                            IntentUtils.startActivity(this, UserInfActivity.class, bundle);
//                            break;
//                        case "terminal":
//                            bundle = new Bundle();
//                            bundle.putString("id", matcher.group(2));
//                            IntentUtils.startActivity(this, TerminalInfActivity.class, bundle);
//                            break;
//
//                        case "beltline":
//                            bundle = new Bundle();
//                            bundle.putString("id", matcher.group(2));
//                            IntentUtils.startActivity(this, BeltlineInfActivity.class, bundle);
//                            break;
//                    }
//
//
//                } else {
//                    ToastUtil.makeText(this, "无效的二维码");
//
//                }
//
////                ToastUtil.makeText(getActivity(),content);
//
//
//            }
//        }
//    }
}
